// pages/sufferer/trevel/travel.js
Page({

  /**
   * 页面的初始数据
   * date 日期
   * number 查询内容
   * page 分页
   * isAll 数据是否查完
   */
  data: {
    date: "",
    number:"",
    page:0,
    isAll:true,
    result: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        pageCur: 'travel',
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data["isAll"] == false) {
      wx.cloud.callFunction({
        name: 'searchSuffererTravel',
        data: {
          number: that.data["number"],
          date: that.data["date"],
          page: that.data["page"],
        },
        success: function (res) {
          that.setData({
            result: that.data["result"].concat(res.result.data)
          });
          if (res.result.data.length == 100) {
            that.setData({
              page: that.data["page"] + 100
            });
          } else {
            that.setData({
              isAll: true
            });
          }

        },
        fail: err => {

        }
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title:"疫轨迹，了解感染者出行活动，发现自身感染风险",
      imageUrl:"/images/share.png",
      path:"/pages/sufferer/travel/travel",
    }
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
copyText: function (e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              icon: 'none',
              title: '复制成功,请到浏览器查看信息源'
            })
          }
        })
      }
    })
  },

  formSubmit: function (e) {
    var that = this;
    var data = e.detail.value;
    if (data.number == "") {
      wx.showToast({
        title: '车次、航班号等不能为空',
        icon: 'none',
        duration: 1500
      })
      }else{
        that.setData({
          page:0,
          date: data.date,
          number: data.number,
          page: 0,
          isAll: false,
          result: [],
        });
      wx.showLoading({
        title: '查询中',
      });
      wx.cloud.callFunction({
        name: 'searchSuffererTravel',
        data:{
          number:that.data["number"],
          date: that.data["date"],
          page: that.data["page"],
        },
        success:function(res){
          wx.hideLoading();
          if (res.result.data.length == 0) {
            wx.showToast({
              icon: 'none',
              title: '暂无数据'
            });
            }
          that.setData({
            result: res.result.data
          });
          if (res.result.data.length == 100){
            that.setData({
              page: that.data["page"] +100
            });
          }else{
            that.setData({
              isAll: true
            });
          }

        },
        fail: err => {
          wx.hideLoading();
          wx.showToast({
            icon: 'none',
            title: '暂无数据'
          })
        }
      })

      }
  },

  formReset:function(e){
    var that = this;
    that.setData({
      date: "",
      number: "",
      page: 0,
      isAll: true,
      result: [],
    })
  },


})